package com.wlcorp.forcazamis.business.storagein.impl;

import java.util.Iterator;
import java.util.List;

import com.wlcorp.forcazamis.business.storagein.IStorageInService;
import com.wlcorp.forcazamis.dao.sample.ISubSampleAssignDAO;
import com.wlcorp.forcazamis.dao.sample.ISubSampleDAO;
import com.wlcorp.forcazamis.dao.storagein.IStorageInDAO;
import com.wlcorp.forcazamis.dao.storagein.IStorageInSampleDAO;
import com.wlcorp.forcazamis.domain.sample.SubSampleAssign;
import com.wlcorp.forcazamis.domain.storagein.StorageIn;
import com.wlcorp.forcazamis.domain.storagein.StorageInSample;
import com.wlcorp.forcazamis.exception.ServiceException;
import com.wlcorp.forcazamis.sys.BaseService;
import com.wlcorp.forcazamis.sys.util.AppConstant;

public class StorageInServiceImpl extends BaseService implements
		IStorageInService {
	
	private IStorageInDAO storageInDAO;
	private IStorageInSampleDAO storageInSampleDAO;
	private ISubSampleDAO subSampleDAO;
	private ISubSampleAssignDAO subSampleAssignDAO;
	public void setStorageInDAO(IStorageInDAO storageInDAO) {
		this.storageInDAO = storageInDAO;
	}
	
	public void setStorageInSampleDAO(IStorageInSampleDAO storageInSampleDAO) {
		this.storageInSampleDAO = storageInSampleDAO;
	}
	
	
	public void setSubSampleDAO(ISubSampleDAO subSampleDAO) {
		this.subSampleDAO = subSampleDAO;
	}
	
	public void setSubSampleAssignDAO(ISubSampleAssignDAO subSampleAssignDAO) {
		this.subSampleAssignDAO = subSampleAssignDAO;
	}

	public List<StorageIn> getStorageInList(StorageIn storageIn) throws ServiceException {
		return storageInDAO.getStorageInList(storageIn);
	}

	public Long getStorageInCount(StorageIn storageIn) throws ServiceException {
		return storageInDAO.getStorageInCount(storageIn);
	}

	public StorageIn getStorageInById(Long storageInId) throws ServiceException {
		if(null == storageInId){
			return null;
		}
		return storageInDAO.getStorageInById(storageInId);
	}

	public void deleteStorageIn(StorageIn storageIn) throws ServiceException {
		storageInDAO.deleteStorageIn(storageIn);
	}

	public void saveStorageIn(StorageIn storageIn) throws ServiceException {
		storageInDAO.saveStorageIn(storageIn);
	}

	public List<StorageInSample> getStorageInSampleList(StorageInSample storageInSample) throws ServiceException {
		return storageInSampleDAO.getStorageInSampleList(storageInSample);
	}

	public Long getStorageInSampleCount(StorageInSample storageInSample) throws ServiceException {
		return storageInSampleDAO.getStorageInSampleCount(storageInSample);
	}

	public void saveStorageInSample(Long storageInId,List<StorageInSample> storageInSampleList) throws ServiceException {
		StorageIn storageIn = storageInDAO.getStorageInById(storageInId);
		if(null != storageIn.getStorageInSampleSet()){
			for(Iterator<?> iter = storageIn.getStorageInSampleSet().iterator();iter.hasNext();){
				StorageInSample storageInSample = (StorageInSample)iter.next();
				storageInSampleDAO.deleteStorageInSample(storageInSample);
			}
		}
		for(Iterator<StorageInSample> iter = storageInSampleList.iterator();iter.hasNext();){
			StorageInSample storageInSample = (StorageInSample)iter.next();
			storageInSample.setSubSample(subSampleDAO.getSubSampleById(storageInSample.getSubSampleId()));
			storageInSample.setStorageIn(storageIn);
			storageInSampleDAO.saveStorageInSample(storageInSample);
		}
	}

	public void withDrawStorageIn(StorageIn storageIn) throws ServiceException {
		for(Iterator<?> iter = storageIn.getStorageInSampleSet().iterator();iter.hasNext();){
			StorageInSample storageInSample = (StorageInSample)iter.next();
			SubSampleAssign subSampleAssign = new SubSampleAssign();
			subSampleAssign.setOwnerId(AppConstant.DEPOT_ID_SAMPLE);
			subSampleAssign.setSubSampleId(storageInSample.getSubSample().getSubSampleId());
			subSampleAssign = subSampleAssignDAO.getSubSampleAssignBySubSampleAndOwner(subSampleAssign);
			if(null == subSampleAssign || null == subSampleAssign.getSampleCount() 
					|| subSampleAssign.getSampleCount() - storageInSample.getInCount() < 0){
				setErrorField(storageIn,AppConstant.BASE_BEAN_STATUS_ERROR,"样品颜色“" +  storageInSample.getSubSample().getCcolorDesc() 
						+ "”的样品剩余库存少于" + storageInSample.getInCount() + "个，无法退单！");
				return;
			}
			subSampleAssign.setSampleCount(subSampleAssign.getSampleCount() - storageInSample.getInCount());
			subSampleAssignDAO.saveSubSampleAssign(subSampleAssign);
		}
		storageIn.setState(AppConstant.BILL_STATE_DRAWBACK);
		storageInDAO.saveStorageIn(storageIn);
	}

}
